﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Purview
{
    /// <summary>
    /// Description: 系统平台权限菜单 -> 菜单管理树页面
    /// Author: tim 2010-03-02
    /// </summary>
    public partial class sys_menu_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidSourceType.Value = (null != Request.QueryString["sourceType"]) ? Request.QueryString["sourceType"].ToString() : "0";
                initializePage();
            }
        }

        private void initializePage()
        {
            createTree();
            trvMenu.ExpandLevel = 1;
        }

        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            createRootNode(ref oRoot);
            trvMenu.Nodes.Add(oRoot);
            createSubNodes(ref oRoot);
        }

        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            oRoot.Text = "系统菜单";
            oRoot.ImageUrl = "../images/toolbar/menu_root.gif";
            oRoot.NavigateUrl = string.Empty;
            oRoot.Target = string.Empty;
            oRoot.ID = "0_0";
        }

        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            MenuBLL menuBLL = new MenuBLL();
            List<MsMenu> lstMenu = menuBLL.GetStairMenuList();

            string menuId = string.Empty;
            string menuName = string.Empty;
            string hasChild = string.Empty;
            string sysSign = string.Empty;
            string lay = "1";
            string oriType = string.Empty;
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;

            foreach (MsMenu eMenu in lstMenu)
            {
                menuId = eMenu.menuId;
                menuName = eMenu.menuName;
                hasChild = eMenu.isHasChild;
                sysSign = eMenu.sysSign;
                oriType = eMenu.oriType;
                oTreeNode = createTreeNode(menuName, menuId, hasChild, sysSign, lay, oriType);
                oRoot.Nodes.Add(oTreeNode);
            }

            lstMenu = null;
            menuBLL.Release();
            menuBLL = null;
        }

        /// <summary>
        /// 创建菜单节点
        /// </summary>
        /// <param name="nodeText"></param>
        /// <param name="menuId"></param>
        /// <param name="hasChild"></param>
        /// <param name="sysSign"></param>
        /// <param name="lay"></param>
        /// <param name="oriType">菜单记录来源类型
        ///         0:由admin账户通过菜单管理功能创建、1:由模块装配功能创建该菜单</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string nodeText, string menuId,
                                                        string hasChild, string sysSign, string lay, string oriType)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            string showText = (sysSign == "0") ? nodeText : nodeText + " [<font color='red'>系统</font>]";            
            oNode.Text = showText;
            string sPageParam = "menuId=" + menuId + "&sys=" + sysSign + "&recOri=" + oriType + "&sourceType=" + _hidSourceType.Value;
            string encodeParam = base64encode(sPageParam);
            oNode.NavigateUrl = "./sys_menu_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/menu.gif";
            oNode.Target = "ifrmEdit";
            oNode.ID = menuId + "_" + "0" + "_" + lay;
            oNode.Type = "root";

            if (hasChild == "1")
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }

            return oNode;
        }

        private string base64encode(string sPageParam)
        {
            string encodeParam = string.Empty;
            if (string.IsNullOrEmpty(sPageParam)) return encodeParam;

            encodeParam = Base64Lib.Encode(sPageParam);
            return encodeParam;
        }


        #region ajax methods

        [global::System.Web.Services.WebMethod]
        public static string GetSubMenus(string menuId, string nodeIndex,
                                        string expandSign, string levelStruct, string sourceType)
        {
            MenuBLL menuBLL = new MenuBLL();
            List<MsMenu> lstMenu = menuBLL.GetSubMenuList(menuId,sourceType);

            var newObj = new {
                OPERTYPE = "DYNLOADNODES",
                NODEINDEX = nodeIndex,
                MENUS = lstMenu,
                EXPSIGN = expandSign,
                LVSTRUCT = levelStruct,
                CURNODEID = menuId
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstMenu = null;
            menuBLL.Release();
            menuBLL = null;
            return serExpr;
        }
                

        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="menuId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string DeleteMenu(string menuId, string nodeIndex)
        {
            MenuBLL menuBLL = new MenuBLL();
            List<string> lstResult = menuBLL.Delete(menuId);

            var newObj = new {
                OPERTYPE = "MENUDELETE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                NODEINDEX = nodeIndex
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            lstResult = null;
            menuBLL.Release();
            menuBLL = null;
            return serExpr;
        }

        /// <summary>
        /// get destination parent node struct tree node ids
        /// </summary>
        /// <param name="dtNodeId"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetNodeLevelStruct(string dtNodeId)
        {
            MenuBLL menuBLL = new MenuBLL();
            string structIds = menuBLL.GetMenuLevelStruct(dtNodeId);
            var newObj = new {
                OPERTYPE = "GETSTRUCT",
                RSTIDS = structIds
            };

            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            newObj = null;
            menuBLL.Release();
            menuBLL = null;
            return serExpr;
        }

        #endregion
    }
}
